<%@page import="interface_layer.ErrorHandler"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%@ page import="database_layer.*" %>
<%@ page import="java.util.*"%>
<%@ page import="processing_layer.*" %>
<%@ page errorPage="exceptionPage.jsp" %>
<%-- 
	This JSP presents all the courses in the system. Depending on the user type (admin or student)
	and the allowed actions (delete the course for an admin, register / unregister for a user)
	the browsing user can check information about the courses, or active the allowed commmands.
 --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255"/>
<link href="css/templatemo_style.css" rel="stylesheet" type="text/css" />
<title>Courses browser</title>
</head>
<body>
<jsp:include page="header_part_1.html"/>
            <div id="templatemo_menu">
                <ul>
                    <li><a href="index.jsp">Home</a></li>
                    <li><a href="about.jsp">About Us</a></li>
<%
                    	String username = (String) session.getAttribute("username");
                        if (null != username)
                        {
%>
                            <li><a href="userPage.jsp">Your profile</a></li>
                            <li><a href="allCourses.jsp" class="current">Courses</a></li>
                    		<li><a href="allUsers.jsp">Users</a></li>
<%
                        }
%>
                </ul>    	
            </div> <!-- end of templatemo_menu -->
<jsp:include page="header_part_2.html"/>
<div id="content">
<%
	// Need to add a session check here?
	String userType = (String) session.getAttribute("userType");
	
	if (null == username || null == userType)
	{
		/* Not logged in = Not authorized */
		ErrorHandler.parseForErrors(Common.UNAUTHORIZED_OPERATION, response, null);
		return;
	}
	
	ArrayList<Course> allCourses = QueryDispatcher.getAllCourses();
	ArrayList<Course> userAvailableCourses = null;
	ArrayList<Course> userRegisteredCourses = null;
	ArrayList<Course> adminCreatedCourses = null;
	
	boolean validAdmin = false;
	boolean validStudent = false;
	
	if (allCourses == null)
	{
		ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);
		return;		
	}
	
	if (0 == allCourses.size())
	{
%>
		<i>There are no courses in the system yet... </i>
<%	
	}
	else
	{
		if (null != userType)
		{
			if (userType.equals(Common.studentType))
			{
				userAvailableCourses = QueryDispatcher.getAllAvailableCourses(username);
				userRegisteredCourses = QueryDispatcher.getRegisteredCourses(username);
				if (null == userRegisteredCourses || null == userRegisteredCourses)
				{
					ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);
					return;
				}
				validStudent = true;
%>
			<b><i>
			This list contains all the courses in the system<br/>
			The <u>available</u> courses for you will present the <u>Register/Unregister</u>
			option. (You can always enter the course details and perform these actions)<br/>
			</i></b>
<%
			}
			
			if (userType.equals(Common.adminType))
			{
				adminCreatedCourses = QueryDispatcher.getAllGeneratedCourses(username);
				if (null == adminCreatedCourses)
				{
					ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);
					return;
				}
				validAdmin = true;
%>
			<b><i>
			This list contains all the courses in the system<br/>
			The courses you've created will be accompanied by a <u>Delete course</u> option.
			(You can always enter the course details and delete it)<br/>
			</i></b>
<%
			}
		}
%>
		<table border="1">
<%
		for (Course c : allCourses)
		{
%>
			<tr>
				<td class="coursesAndUsersTable" id="coursesAndUsersDescription">	
					
<%
			out.println("<b>Name</b>: " + c.name + "<br/>");
			out.println("<b>Credit pts</b>: " + c.creditPoints + "<br/>");
			out.println("<b>Group number</b>: " + c.groupNumber + "<br/>");
			out.println("<br/><a href='courseDetails.jsp?course=" + c.name + "'>More about this course</a>");
%>
					
				</td>
				<td class="coursesAndUsersTable" id="coursesAndUsersManagement">
<%		
			if (validStudent)
			{
				boolean userCanRegister = userAvailableCourses.contains(c);
				boolean userCanUnregister = userRegisteredCourses.contains(c);
				if (userCanRegister || userCanUnregister)
				{
%>
					<form method="get" action="CourseRegistration">
					<input type="hidden" name="course" value="<%= c.name %>"/>
<%
					if (userCanRegister)
					{
%>					
						<input type="hidden" name="operation" value="<%= "Register" %>"/>
						<input type="submit" value="Register" />	
<%
					}
	
					if (userCanUnregister)
					{
%>					
						<input type="hidden" name="operation" value="<%= "Unregister" %>"/>
						<input type="submit" value="Unregister" />	
<%		
					}
%>
					</form>
<%
				}
				else
				{
%>
					<i>Course doesn't meet registration requirements</i>
<%
				}
			}
			
			if (validAdmin)
			{
				if (adminCreatedCourses.contains(c))
				{
%>
					<form method="get" action="removeCourse">
					<input type="hidden" name="courseToRemove" value="<%= c.name %>"/>
					<input type="submit" value="Remove the course" />	
					</form>
<%
				}
				else
				{
%>
					<i>You're not the creator of this course</i>
<%				
				}
			}
%>
				</td>
			</tr>
<%
		}
%>
		</table>
<%
	}
%>
</div>
<jsp:include page="sidebar.jsp"/>
<jsp:include page="footer.html"/>
</body>
</html>